//
//  OpenCoursesVC.swift
//  Auscultation
//
//  Created by 张文博 on 2021/4/6.
//

import UIKit
import JXSegmentedView

class OpenCoursesVC: BaseVC {
    let totalItemWidth: CGFloat = 150
    var segmentedDataSource: JXSegmentedBaseDataSource?
    let segmentedView = JXSegmentedView()
    lazy var listContainerView: JXSegmentedListContainerView! = {
        return JXSegmentedListContainerView(dataSource: self)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        let titles = ["视频", "文档"]
//        let titleDataSource = JXSegmentedTitleDataSource()
//        titleDataSource.itemWidth = totalItemWidth/CGFloat(titles.count)
        let titleDataSource = JXSegmentedTitleDataSource()
        titleDataSource.itemSpacing = 15

        titleDataSource.titleNormalFont = UIFont.systemFont(ofSize: 18)
        titleDataSource.titleSelectedFont = UIFont.boldSystemFont(ofSize: 21)
        titleDataSource.titleNormalColor = UIColor.init(lgf_HexString: "#A0A0A0")
        titleDataSource.titleSelectedColor = UIColor.black
        titleDataSource.isTitleZoomEnabled = true

        titleDataSource.titles = titles
        //配置指示器
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = UIImage(named: "矩形")
        indicator.indicatorWidth = 24
        indicator.indicatorHeight = 18

        segmentedDataSource = titleDataSource

        segmentedView.dataSource = titleDataSource
        segmentedView.indicators = [indicator]

        //segmentedViewDataSource一定要通过属性强持有！！！！！！！！！
        segmentedView.delegate = self
        self.navTitle = ""
        let style = LGFNavigationBarStyle.lgf()
        style.lgf_LeftBtnImageLight = UIImage(named: "darkback")!
        style.lgf_ShowTitleView = true
        self.lgf_NavigationBar.lgf_line?.isHidden = true

        self.lgf_NavigationBar.lgf_TitleView?.addSubview(segmentedView)
        self.lgf_NavigationBar.lgf_Reload()
        segmentedView.listContainer = listContainerView
        view.addSubview(listContainerView)

        for indicaotr in segmentedView.indicators {
            if (indicaotr as? JXSegmentedIndicatorLineView) != nil ||
                (indicaotr as? JXSegmentedIndicatorDotLineView) != nil ||
                (indicaotr as? JXSegmentedIndicatorDoubleLineView) != nil ||
                (indicaotr as? JXSegmentedIndicatorRainbowLineView) != nil ||
                (indicaotr as? JXSegmentedIndicatorImageView) != nil ||
                (indicaotr as? JXSegmentedIndicatorTriangleView) != nil {
//                navigationItem.rightBarButtonItem = UIBarButtonItem(title: "指示器位置切换", style: UIBarButtonItem.Style.plain, target: self, action: #selector(didIndicatorPositionChanged))
                break
            }
        }

        if (segmentedDataSource as? JXSegmentedTitleImageDataSource) != nil {
//            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "设置", style: UIBarButtonItem.Style.plain, target: self, action: #selector(didSetingsButtonClicked))
        }
        
        
        if let _ = segmentedDataSource as? JXSegmentedNumberDataSource {
//            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "刷新", style: UIBarButtonItem.Style.plain, target: self, action: #selector(hanldeNumberRefresh))
        }
    }
    

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        //处于第一个item的时候，才允许屏幕边缘手势返回
        navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        //离开页面的时候，需要恢复屏幕边缘手势，不能影响其他页面
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        segmentedView.frame = CGRect(x: 0, y: 0, width: 150, height: 50)
        segmentedView.lgf_X = 110 / 2
        listContainerView.frame = CGRect(x: 0, y: UIDevice.lgf_NavBarH(), width: view.bounds.size.width, height: view.bounds.size.height - UIDevice.lgf_NavBarH())
    }
}

extension OpenCoursesVC: JXSegmentedViewDelegate {
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if let dotDataSource = segmentedDataSource as? JXSegmentedDotDataSource {
            //先更新数据源的数据
            dotDataSource.dotStates[index] = false
            //再调用reloadItem(at: index)
            segmentedView.reloadItem(at: index)
        }
        navigationController?.interactivePopGestureRecognizer?.isEnabled = (segmentedView.selectedIndex == 0)
    }
}

extension OpenCoursesVC: JXSegmentedListContainerViewDataSource {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
            
            return titleDataSource.dataSource.count
        }
        return 0
    }

    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        
        if index == 0 {
            return VideoViewController()
        }else{
            return DocumentViewController()
        }
    }
}


